import { defineStore } from "pinia"
import { useRouter } from "vue-router"
import { deleteTasks, searchTask } from "../../api"
import {
    ElMessageBox,
    ElMessage,
} from 'element-plus'
//   @element-plus/icons-v
export const useAllTaskStore = defineStore('AllTask', {
    state() {
        return {
            userWorksList: [],
            userWorksListCopy: [],
            isGroupSearch: false,//是否结合查询，标识是否以查询回来的进行分页
            currentPage: 1, //当前的页数
            pageSize: 10,//当前页面显示的数据大小
            searchButtonDisabdled: false,//查询按钮禁用标识
            resetButtonDisabdled: false,//重置按钮禁用标识
            exportButtonDisabdled: false,//导出按钮禁用标识
            pageButtonDisabdled: false,//分页按钮禁用标识
            formInline: {
                title: "",
                content: "",
                taskType: "",
                taskStatus: "",
                taskDates: [],
                createTime: "",
                endTime: ""
            },//查询的表单数据
            showMask: true,//显示遮罩，
            tagTypes: ['自主创建', '被指派'],//任务状态
            taskStatus: ['进行中（待办）', '已完成（已办）', '已到期（未完成）'],//任务状态

        }
    },
    getters: {
        // 获取分页开始与结束数值
        pageNuber(state) {
            return {
                pageStart: (state.currentPage - 1) * state.pageSize,
                pageEnd: state.pageSize - 1
            }

        }
    },
    actions: {


        // 删除任务
        deleteTasks(data) {

            this.showMask = true//显示遮罩
            deleteTasks(JSON.stringify(data)).then(res => {
                if (res.data.code != 401) {
                    this.userWorksList = this.userWorksList.filter((val) => {
                        return val.id != rowData.id
                    })
                    this.userWorksListCopy = this.userWorksListCopy.filter((val) => {
                        return val.id != rowData.id
                    })

                    ElMessage.success('已删除')
                } else {
                    ElMessage.error(res.data.msg)
                }
            })
            let timer = setTimeout(() => {
                this.showMask = false//隐藏遮罩
                clearTimeout(timer)
            }, 1000)
        },
        // 查询任务
        searchTask() {
            let userId;
            try {
                userId = JSON.parse(localStorage.getItem("userInfo")).data.userId//获取本地的用户Id
                if (userId == null || userId == undefined || userId == "") throw ("还未登录，即将跳转登录页面")

            } catch (error) {
                ElMessage.error(error)
                const router = useRouter()
                setTimeout(() => {
                    router.push({ path: "/login" })
                }, 1000)
            }



            if (this.formInline.taskDates.length > 1) {
                this.formInline.createTime = this.formInline.taskDates[0]
                this.formInline.endTime = this.formInline.taskDates[1]
            } else if (this.formInline.taskDates.length > 0) {
                if (this.formInline.taskDates[0].length != 0) {
                    this.formInline.createTime = this.formInline.taskDates[0]
                } else if (this.formInline.taskDates[1].length != 0) {
                    this.formInline.endTime = this.formInline.taskDates[1]
                }
            }
            let { title, content, createTime, endTime, taskType, taskStatus } = this.formInline


            let obj = { title, content, createTime, endTime, taskType, taskStatus }
          
            let objtoJson = { 
                userId:userId, 
                title:title==undefined||title==''?null:title, 
                content:content==undefined||content==''?null:content, 
                createTime:createTime==undefined||createTime==''?null:createTime, 
                endTime:endTime==undefined||endTime==''?null:endTime 
            }
            this.showMask = true//显示遮罩

            this.searchButtonDisabdled = true//禁用查询
            this.resetButtonDisabdled = true//禁用重置
            this.exportButtonDisabdled = true//禁用导出
            this.pageButtonDisabdled = true//禁用分页

            // if(title.length!=0||
            //     content.length!=0||
            //     createTime.length!=0||
            //     endTime.length!=0){

            searchTask(JSON.stringify(objtoJson)).then(res => {
                if (res.data.code != 500 && res.data != null
                    && res.data != "" &&
                    res.data.code != 401) {

                    this.userWorksList = res.data.data


                    if (taskType != undefined && taskType != "") {
                        // 查询自主创建  被指派
                        this.userWorksList = this.userWorksList.filter((val) => {
                            let type = this.tagTypes[val.appointorId == null ? 0 : 1]
                            return type.indexOf(obj.taskType) != -1
                        })
                    }

                    if (taskStatus != undefined && taskStatus != "" && this.userWorksListCopy.length) {
                        // 查询任务状态  已完成 进行中 已逾期 
                        this.formInline.taskStatus
                        this.userWorksList = this.userWorksList.filter((val) => {
                            return val.workStatusName.indexOf(obj.taskStatus) != -1
                        })
                    }
                    let objPage = this.pageNuber
                    //    最后在满足所有组合条件中执行分页
                    this.userWorksListCopy = [...this.userWorksList]
                    this.userWorksList = this.userWorksList.slice(objPage.pageStart, objPage.pageStart + objPage.pageEnd)




                } if (res.data.code == 500) {
                    ElMessage.success(res.data.msg)
                } else if (res.data.code != 200) {
                    ElMessage.error(res.data.msg)
                }
            })
            // }
            let timer = setTimeout(() => {
                this.showMask = false//隐藏遮罩
                this.searchButtonDisabdled = false,//取消禁用查询
                    this.resetButtonDisabdled = false,//取消禁用重置
                    this.exportButtonDisabdled = false,//取消禁用导出
                    this.pageButtonDisabdled = false,//取消禁用分页
                    clearTimeout(timer)
            }, 1300)






        }

    }

})